unit frmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack,
  IdSSL, IdSSLOpenSSL, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP, Buttons;

type
  TMainform = class(TForm)
    pages: TPageControl;
    tbLastFm: TTabSheet;
    HTTP: TIdHTTP;
    idSSL: TIdSSLIOHandlerSocketOpenSSL;
    sEdit: TEdit;
    sBtn: TButton;
    ListBox1: TListBox;
    lfmNext: TBitBtn;
    procedure sBtnClick(Sender: TObject);
    procedure lfmNextClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Mainform: TMainform;

implementation

{$R *.dfm}

procedure LastFMSearch(const Artists: TStrings; Artist: string);
var
  S : string;
  sData : TStringList;
  I : Integer;
begin
  sData := TStringList.Create;
  Artist := StringReplace(Artist, ' ', '%20', [rfReplaceAll]);
  Artist := AnsiToUTF8(Artist);
 try
  sData.Text := Utf8TOAnsi(MainForm.HTTP.Get('http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=' + Artist + '&api_key=b25b959554ed76058ac220b7b2e0a026'));
 except ShowMessage('HTTP Error');
 end;

  for i := 0 to sData.Count -1 do
    begin
      if Pos('<name>', sData[i]) <> 0 then
      begin
        S := sData[i];
        S := Copy(s, Pos('>', s) +1, MaxInt);
        Delete(s, Pos('</', s), MaxInt);
        Artists.Add(s);
      end;
    end;
end;

procedure TMainform.lfmNextClick(Sender: TObject);
begin
 if ListBox1.ItemIndex = -1 then exit;
end;

procedure TMainform.sBtnClick(Sender: TObject);
begin
  if Length(sEdit.Text) = 0 then Exit;

  ListBox1.Clear;
  LastFMSearch(ListBox1.Items, sEdit.Text);
end;

end.
